/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.buttonmanager.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Repository;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButton;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;
import net.sf.ehcache.CacheManager;

/**
 * ClassName:PlatButtonService <br/>
 * Date: 2015年5月8日 下午4:28:49 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Repository("system.modules.system.PlatButtonService")
public class PlatButtonService extends BaseService {
	
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	@Transactional
	@CachePut(value="ButtonCache",key="#button.getBtnId()")
	public PlatButton save(PlatButton button) {
		String btnId = button.save();
		button.setBtnId(btnId);
		return button;
	}

	@Transactional
	@CachePut(value="ButtonCache",key="#button.getBtnId()")
	public PlatButton update(PlatButton button) {
		return button.update();
	}
	
	@Transactional
	@CachePut(value="ButtonCache",key="#button.getBtnId()")
	public PlatButton updateButtonOnly(PlatButton button) {
		String hql = "update PlatButton b set b.btnName = ? ,b.icon = ? , b.enabled = ? , b.btnEvent = ? where btnId = ?";
		int result = hibernateDAO.executeUpdate(hql, new Object[]{button.getBtnName(),button.getIcon(),button.getEnabled(),button.getBtnEvent(),button.getBtnId()});
		return result > 0 ? button : null;
	}
	
	@Transactional
	@CacheEvict(value="ButtonCache",key="#button.getBtnId()")
	public PlatButton delete(PlatButton button) {
		return button.delete();
	}

	@Transactional
	@CachePut(value="ButtonCache",key="#button.getBtnId()")
	public PlatButton saveOrUpdate(PlatButton button) {
		if(button.getBtnId() != null){
			return updateButtonOnly(button);
		}
		return button.saveOrUpdate();
	}
	
	@Transactional
	public List<PlatButton> getButtons(Set<PlatRole> roles){
		List params = new ArrayList();
		if(roles.size() == 0){
			return null;
		}
		String hql = "";
		for(PlatRole r : roles){
			hql+=("?,");
			params.add(r.getRoleId());
		}
		hql= hql.substring(0,hql.length()-1);
		List<PlatButton> buttons = (List<PlatButton>) super.getHibernateDAO().findAll("select distinct u from PlatButton as u , PlatRole as r where u.btnId in elements(r.btns) and r.id in ("+hql+") order by u.btnId",params);
		return buttons;
	}
	
	@Transactional
	public List<PlatButton> getAllButtons(){
		List<PlatButton> buttons = (List<PlatButton>) super.getHibernateDAO().findAll("from "+PlatButton.class.getName());
		return buttons;
	}
	
	@Transactional
	public List<PlatButton> getButtons(Set<PlatRole> roles,String btnGid){
		List params = new ArrayList();
		if(roles.size() == 0){
			return null;
		}
		String hql = "";
		for(PlatRole r : roles){
			hql+=("?,");
			params.add(r.getRoleId());
		}
		params.add(btnGid);
		hql= hql.substring(0,hql.length()-1);
		List<PlatButton> buttons = (List<PlatButton>) super.getHibernateDAO().findAll("select distinct u from PlatButton as u , PlatRole as r where u.btnId in elements(r.btns) and r.id in ("+hql+") and u.btnGroup.btnGId = ? order by u.btnId",params);
		return buttons;
	}

}
